﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System.Web;
using System.Configuration;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// AGV避障时长统计报表运算
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IWMSYSController : ControllerBase
    {
        private readonly ILogger<IWMBZInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_t;


        public IWMSYSController(ILogger<IWMBZInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_t = _configuration.GetConnectionString("WMSDbConnection_Test");

        }

        /// <summary>
        /// AGV避障时长统计报表运算
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostYS([FromBody] IWMBZInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            IWMBZInfoResponseDto re = new IWMBZInfoResponseDto();
            string sqlstr = "";
            string machine_address = "5-A-04";
            string machine_address_t = "5-A-03";
            string machine_AGV= "A004";


            try
            {

                sqlstr = "select * from  Temp_iwms_0429  where (is_push is null or is_push='') and machine_address='"+ machine_address + "' order by creation_date;";
                DataTable dt = DB.Query(sqlstr, con_t);
                if (dt.Rows.Count>0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        sqlstr = "select * from Temp_iwms_0429  where  (is_push is null or is_push='') and task_no='"+ row["task_no"].ToString() + "' ";
                        DataTable dt_check = DB.Query(sqlstr, con_t);
                        if (dt_check.Rows.Count>0)
                        {
                            DateTime endtime = DB.ToDateTime(row["creation_date"].ToString()).AddMinutes(3);
                            DateTime endtime2 = DB.ToDateTime(row["creation_date"].ToString()).AddMinutes(8);

                            sqlstr = @"select * from Temp_iwms_0429  where machine_address='"+ machine_address + "' and  creation_date_t>='"+ row["creation_date"].ToString() + "' and creation_date_t<'" + endtime.ToString()+ "'";
                            DataTable dt_zhua = DB.Query(sqlstr, con_t);
                            if (dt_zhua.Rows.Count==2)
                            {
                                sqlstr = @"select * from Temp_iwms_0429  where machine_address='" + machine_address_t + "' and  creation_date_t>='" + row["creation_date"].ToString() + "' and creation_date_t<'" + endtime2.ToString() + "'";
                                DataTable dt_tg = DB.Query(sqlstr, con_t);
                                if (dt_tg.Rows.Count<=0)
                                {
                                    sqlstr = @"select  agvname,updatatime,case when mttype='备品'  then 1 else  0 end mttype,case when lmttype='备品'  then 1 else  0 end lmttype,
case when cmttype='备品'  then 1 else  0 end cmttype from Temp_iwms_0429_AGV  where  updatatime_t>='" + row["creation_date"].ToString() + "'  and updatatime_t<='" + endtime.ToString() + "' and  agvname='"+ machine_AGV + "' order by updatatime  desc ";
                                    DataTable dt_agv = DB.Query(sqlstr, con_t);
                                    if (dt_agv.Rows.Count>0)
                                    {
                                        string agv_time = "";
                                        int js = DB.ToInt(dt_agv.Rows[0]["mttype"].ToString()) + DB.ToInt(dt_agv.Rows[0]["lmttype"].ToString()) + DB.ToInt(dt_agv.Rows[0]["cmttype"].ToString());                                       
                                        if (js<=1)
                                        {
                                            agv_time = dt_agv.Rows[0]["updatatime"].ToString();
                                            sqlstr = "update Temp_iwms_0429  set qty=1,is_push='Y'   where task_no='" + row["task_no"].ToString() + "'; ";
                                            strSqlList = new List<string>();
                                            strSqlList.Add(sqlstr.ToString());
                                            sqlstr = "update Temp_iwms_0429_AGV  set task_no='"+ row["task_no"].ToString() + "'  where updatatime_t='" + agv_time + "'  and agvname='"+ machine_AGV + "' ; ";
                                            strSqlList.Add(sqlstr.ToString());
                                            DB.ExecuteNoQueryTran(strSqlList, con_t);
                                        }
                                    }                                 
                                }
                                foreach (DataRow rows in dt_zhua.Rows)
                                {
                                    sqlstr = "update Temp_iwms_0429  set is_push='Y'   where task_no='" + rows["task_no"].ToString() + "'; ";
                                    strSqlList = new List<string>();
                                    strSqlList.Add(sqlstr.ToString());
                                    DB.ExecuteNoQueryTran(strSqlList, con_t);
                                }

                            }
                            sqlstr = "update Temp_iwms_0429  set  is_push='Y'   where task_no='" + row["task_no"].ToString() + "'; ";
                            strSqlList = new List<string>();
                            strSqlList.Add(sqlstr.ToString());
                            DB.ExecuteNoQueryTran(strSqlList, con_t);
                        }
                    }
                    //if (strSqlList.Count > 0)
                    //{
                    //    bool A = DB.ExecuteNoQueryTran(strSqlList, con_t);
                    //}
                }                        
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.code = 0;
               
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
